package com.qqtech.qquan.mall.controller;

import java.sql.Timestamp;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.mall.model.MallSubject;
import com.qqtech.qquan.mall.model.MallSubjectQuery;
import com.qqtech.qquan.mall.model.MallSubjectVo;
import com.qqtech.qquan.mall.service.MallSubjectService;
import com.qqtech.qquan.sys.controller.BaseAdminController;

@Controller
@RequestMapping(value = "/mall/subject")
public class MallSubjectController extends BaseAdminController {
	@Resource
	private MallSubjectService mallSubjectService;
    
	@Override
	protected BaseService<MallSubject> getBaseService() {
		return mallSubjectService;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		return "/mall/subject-list";
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(MallSubjectQuery query, HttpServletRequest request,
			HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(),
				new PageOrder(Direction.ASC, "oidx"));
		Page<MallSubjectVo> page = mallSubjectService.queryPageList(query,
				pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model) {
		return "/mall/subject-update";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Result doAdd(MallSubjectVo mallSubjectVo, HttpServletRequest request,
			HttpServletResponse response) {
		Result ret = new Result();
		if (mallSubjectVo == null) {
			ret.addErr("参数不能为空");
		} else {
			if (StringUtil.isBlank(mallSubjectVo.getTitle())) {
				ret.addErr("请输入专区名称");
			}
			if (StringUtil.isBlank(mallSubjectVo.getImg())) {
				ret.addErr("请上传专区广告图片");
			}
			if (mallSubjectVo.getStatus() == null) {
				ret.addErr("状态不能为空");
			}
		}
		if (ret.checkForm()) {
			Timestamp now = TimeUtil.now();
			MallSubject mallSubject = new MallSubject();
			mallSubject.setTitle(mallSubjectVo.getTitle().trim());
			mallSubject.setOidx(mallSubjectVo.getOidx());
			mallSubject.setStatus(mallSubjectVo.getStatus());
			mallSubject.setImg(mallSubjectVo.getImg());
			mallSubject.setNewAt(now);
			mallSubject.setUpdAt(now);
			if(StringUtil.isBlank(mallSubjectVo.getRemark())){
				mallSubjectVo.setRemark("");
			}
			mallSubject.setRemark(mallSubjectVo.getRemark());
			mallSubjectService.insert(mallSubject);
		}
		return ret;
	}
	
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) {
		MallSubjectVo mallSubjectVo = mallSubjectService.getById(id);
		request.setAttribute("mallSubjectVo", mallSubjectVo);
		return "/mall/subject-update";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Result doUpdate(MallSubjectVo mallSubjectVo) {
		Result ret = new Result();
		if (mallSubjectVo == null || mallSubjectVo.getId() == null) {
			ret.addErr("参数不能为空");
		} else {
			if (StringUtil.isBlank(mallSubjectVo.getTitle())) {
				ret.addErr("请输入专区名称");
			}
			if (StringUtil.isBlank(mallSubjectVo.getImg())) {
				ret.addErr("请上传专区广告图片");
			}
			if (mallSubjectVo.getStatus() == null) {
				ret.addErr("状态不能为空");
			}
		}
		if (ret.checkForm()) {
			Timestamp now = TimeUtil.now();
			MallSubject mallSubject = new MallSubject();
			mallSubject.setId(mallSubjectVo.getId());
			mallSubject.setTitle(mallSubjectVo.getTitle().trim());
			mallSubject.setOidx(mallSubjectVo.getOidx());
			mallSubject.setStatus(mallSubjectVo.getStatus());
			mallSubject.setImg(mallSubjectVo.getImg());
			mallSubject.setUpdAt(now);
			if(StringUtil.isBlank(mallSubjectVo.getRemark())){
				mallSubjectVo.setRemark("");
			}
			mallSubject.setRemark(mallSubjectVo.getRemark());
			mallSubjectService.updateById(mallSubject);
		}
		return ret;
	}
	
	@RequestMapping(value = "/imgUpload", method = RequestMethod.GET)
	public String imgUpload(Model model) {
		return "/mall/subject-imgUpload";
	}
}
